package org.dromara.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.DemoExample2;
import org.dromara.demo.domain.DemoExample2Line;
import org.dromara.demo.mapper.DemoExample2LineMapper;
import org.dromara.demo.mapper.DemoExample2Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 测试主从Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-03-24
 */
@RequiredArgsConstructor
@Service
public class DemoExample2ServiceImpl extends BaseService {

  private final DemoExample2Mapper baseMapper;

  private final DemoExample2LineMapper lineMapper;

  /**
   * 获取所有主表数据
   */
  public List<DemoExample2> list() {
    List<DemoExample2> list = baseMapper.selectList();
    return MapstructUtils.convert(list, DemoExample2.class);
  }

  /**
   * 查询测试主从
   */
  public DemoExample2 queryById(Long id) {
    return baseMapper.selectById(id);
  }

  /**
   * 查询测试主从列表
   */
  public TableDataInfo<DemoExample2> queryPageList(DemoExample2 bo, PageQuery pageQuery) {
    LambdaQueryWrapper<DemoExample2> lqw = buildQueryWrapper(bo);
    Page<DemoExample2> result = baseMapper.selectPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  /**
   * 查询测试主从列表
   */
  public List<DemoExample2> queryList(DemoExample2 bo) {
    LambdaQueryWrapper<DemoExample2> lqw = buildQueryWrapper(bo);
    return baseMapper.selectList(lqw);
  }

  private LambdaQueryWrapper<DemoExample2> buildQueryWrapper(DemoExample2 bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<DemoExample2> lqw = Wrappers.lambdaQuery();
    lqw.like(StringUtils.isNotBlank(bo.getName()), DemoExample2::getName, bo.getName());
    lqw.eq(bo.getDeptId() != null, DemoExample2::getDeptId, bo.getDeptId());
    lqw.eq(bo.getUserId() != null, DemoExample2::getUserId, bo.getUserId());
    return lqw;
  }

  /**
   * 新增测试主从
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean insertByBo(DemoExample2 bo) {
    if (!validEntityBeforeSave(bo)) {
      return false;
    }
    Boolean flag;
    flag = baseMapper.insertOrUpdate(bo);
    List<DemoExample2Line> lineList = bo.getLine();
    for (DemoExample2Line line : lineList) {
      line.setId(bo.getId());
    }
    flag = this.insertOrUpdateLineBatch(lineList);
    return true;
  }

  /**
   * 修改测试主从
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean updateByBo(DemoExample2 bo) {
    if (!validEntityBeforeSave(bo)) {
      return false;
    }
    Boolean flag;
    flag = baseMapper.insertOrUpdate(bo);
    List<DemoExample2Line> lineList = bo.getLine();
    for (DemoExample2Line line : lineList) {
        line.setId(bo.getId());
    }
    flag = this.insertOrUpdateLineBatch(lineList);
    return true;
  }

  /**
   * 保存前的数据校验
   */
  private Boolean validEntityBeforeSave(DemoExample2 entity) {
    //TODO 做一些数据校验,如唯一约束

    return true;
  }

  /**
   * 批量删除测试主从
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    this.deleteLineWithValidByIds(ids, true);
    return baseMapper.deleteBatchIds(ids) > 0;
  }

  /**
   * 查询测试从========================================操作line表开始===============================
   */
//  public DemoExample2Line queryLineById(Long lineId) {
//    return  lineMapper.selectVoById(lineId,DemoExample2Line.class);
//  }

  /**
   * 查询测试从列表
   */
  public TableDataInfo<DemoExample2Line> queryLinePageList(DemoExample2Line bo, PageQuery pageQuery) {
    LambdaQueryWrapper<DemoExample2Line> lqw = buildLineQueryWrapper(bo);
    Page<DemoExample2Line> result = lineMapper.selectPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  private LambdaQueryWrapper<DemoExample2Line> buildLineQueryWrapper(DemoExample2Line bo) {
    LambdaQueryWrapper<DemoExample2Line> lqw = Wrappers.lambdaQuery();
    lqw.eq(bo.getId() != null, DemoExample2Line::getId, bo.getId());
    return lqw;
  }

  /**
   * 新增测试从
   */

  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean insertOrUpdateLineBatch(Collection<DemoExample2Line>  list) {
    Boolean flag;
    if (list.size() > 0){
      flag = lineMapper.insertOrUpdateBatch(list);
    }else{
      flag = false;
    }
    return flag;
  }

  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean insertLineByBo(DemoExample2Line bo) {
    validLineEntityBeforeSave(bo);
    boolean flag = lineMapper.insert(bo) > 0;
    return flag;
  }

  /**
   * 修改测试从
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean updateLineByBo(DemoExample2Line bo) {
    validLineEntityBeforeSave(bo);
    return lineMapper.updateById(bo) > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validLineEntityBeforeSave(DemoExample2Line entity) {
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除通过子表lineId
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean deleteWithValidByLineIds(Collection<Long> lineIds, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    return lineMapper.deleteBatchIds(lineIds) > 0;
  }

  /**
   * 通过主表ids删除子表
   *
   * @param ids
   * @param isValid
   * @return
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean deleteLineWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    LambdaQueryWrapper<DemoExample2Line> lqw = new LambdaQueryWrapper<>();
    lqw.in(DemoExample2Line::getId, ids);
    return lineMapper.delete(lqw) > 0;
  }
}
